package ynu.edu.controller;

import jakarta.annotation.Resource;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import ynu.edu.entity.Cart;
import ynu.edu.entity.User;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/cart")
public class CartController {
    @Resource
    private RestTemplate restTemplate;

    @Resource
    private DiscoveryClient discoveryClient;

    @GetMapping("/getCartById/1/{userId}")
    public Cart getCartById(@PathVariable("userId") Integer userId){
        List<ServiceInstance> instanceList =discoveryClient.getInstances("provider-service");
        ServiceInstance instance= instanceList.getFirst();
        Cart cart = new Cart();
        List<String> goods=new ArrayList<>();
        goods.add("蓝色卫衣");
        goods.add("Python入门与实践");
        goods.add("笔记本电脑");
        cart.setGoodList(goods);
        User u =restTemplate.getForObject("http://"+instance.getHost()+":"+instance.getPort()+"/user/getUserById/"+userId.toString(), User.class);
        cart.setUser(u);
        return cart;
    }

}
